package com.fruit.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

/**
 * HttpClientManagerT
 * @author Administrator 说明：日期处理 创建人：FH Q313596790 
 * @version 2017.12.01 11:40
 */
public class HttpClientManagerT {
	
	/**
	 * 获取数据
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getData(String url) {
		HttpGet httpget = new HttpGet(url);

		return getData(httpget);
	}

	/**
	 * 获取数据
	 * @param httpget httpget
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getData(HttpGet httpget) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * @param url url
	 * @param xmlParam xmlParam
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(String url, String xmlParam) {

		HttpPost httpPost = new HttpPost(url);

		return postData(httpPost, xmlParam);
	}

	/**
	 * 获取数据
	 * @param httpPost httpPost
	 * @param xmlParam xmlParam
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(HttpPost httpPost, String xmlParam) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * @param httpPost httpPost
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(HttpPost httpPost) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * @param url url
	 * @param params params
	 * @param xmlParam xmlParam
	 * @param factory factory
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postSslData(String url, Map<String, String> params, String xmlParam,
			LayeredConnectionSocketFactory factory) {

		HttpPost httpPost = new HttpPost(url);
		if (params != null) {
			for (String k : params.keySet()) {
				httpPost.addHeader(k, params.get(k));
			}
		}

		return postSslData(httpPost, xmlParam, factory);
	}

	/**
	 * 获取数据
	 * @param httpPost httpPost
	 * @param xmlParam xmlParam
	 * @param factory factory
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postSslData(HttpPost httpPost, String xmlParam, LayeredConnectionSocketFactory factory) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient(factory);

		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static HttpPost getPostMethod(String url) {
		HttpPost pmethod = new HttpPost(url); // 设置响应头信�?
		pmethod.addHeader("Connection", "keep-alive");
		pmethod.addHeader("Accept", "*/*");
		pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
		pmethod.addHeader("Cache-Control", "max-age=0");
		pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

		return pmethod;
	}
}
